/*
 * @(#)Utilities.java	11/06/02
 * Copyleft 2011. All rights reversed.
 *
 * This file is part of AverageGrade.
 *
 * AverageGrade is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * AverageGrade is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with AverageGrade. If not, see <http://www.gnu.org/licenses/>.
 */

package net.grade.averagegrade.util;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Provides methods for common operations, such as getting the contents
 * of the clipboard, rounding numbers and getting <code>Date</code> objects.
 */
public final class Utilities {

    /**
     * Tries to get the current contents of the clipboard as a String. If this
     * is not possible, for whatever reason, <code>null</code> will be returned instead.
     *
     * @return The contents of the clipboard as a String, or <code>null</code>.
     */
    public static String getClipboardContents() {
        final Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        final Transferable inClipboard = systemClipboard.getContents(null);

        if(inClipboard == null || !inClipboard.isDataFlavorSupported(DataFlavor.stringFlavor))
            return null;

        try {
            return (String)inClipboard.getTransferData(DataFlavor.stringFlavor);
        } catch(final Exception ignored) { return null; }
    }

    /**
     * Rounds the specified double value to the number of decimals
     * specified as an argument.
     *
     * @param value    The double value that is to be rounded.
     * @param decimals The number of decimals to round off to.
     * @return The rounded value, with <code>decimals</code> decimals.
     */
    public static double round(final double value, final int decimals) {
        final double decimalExp = Math.pow(10, decimals);
        return Math.round(value * decimalExp) / decimalExp;
    }

    /**
     * Gets a <code>Date</code> object representing today's date.
     *
     * @return A <code>Date</code> object for today's date.
     */
    public static Date getToday() {
        return GregorianCalendar.getInstance().getTime();
    }

    /**
     * Gets a <code>Date</code> object representing the date
     * specified with the parameters.
     *
     * @param year  The value to use for year.
     * @param month The 0-indexed value to use for month, i.e. january is 0.
     * @param day   The value to use for day.
     * @return A <code>Date</code> object representing the specified date.
     */
    public static Date getDate(final int year, final int month, final int day) {
        final Calendar calendar = GregorianCalendar.getInstance();
        calendar.set(year, month, day);
        return calendar.getTime();
    }
}